package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BsLotteryHistory;
import com.ruoyi.system.domain.SysElectronic;
import com.ruoyi.system.mapper.BsLotteryHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BsLotteryMapper;
import com.ruoyi.system.domain.BsLottery;
import com.ruoyi.system.service.IBsLotteryService;

/**
 * 抽奖号码Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-25
 */
@Service
public class BsLotteryServiceImpl implements IBsLotteryService 
{
    @Autowired
    private BsLotteryMapper bsLotteryMapper;

    @Autowired
    private BsLotteryHistoryMapper bsLotteryHistoryMapper;

    /**
     * 查询抽奖号码
     * 
     * @param lotteryId 抽奖号码主键
     * @return 抽奖号码
     */
    @Override
    public BsLottery selectBsLotteryByLotteryId(Long lotteryId)
    {
        return bsLotteryMapper.selectBsLotteryByLotteryId(lotteryId);
    }

    /**
     * 查询抽奖号码列表
     * 
     * @param bsLottery 抽奖号码
     * @return 抽奖号码
     */
    @Override
    public List<BsLottery> selectBsLotteryList(BsLottery bsLottery)
    {
        return bsLotteryMapper.selectBsLotteryList(bsLottery);
    }

    /**
     * 新增抽奖号码
     * 
     * @param bsLottery 抽奖号码
     * @return 结果
     */
    @Override
    public int insertBsLottery(BsLottery bsLottery)
    {
        bsLottery.setCreateTime(DateUtils.getNowDate());
        return bsLotteryMapper.insertBsLottery(bsLottery);
    }

    /**
     * 修改抽奖号码
     * 
     * @param bsLottery 抽奖号码
     * @return 结果
     */
    @Override
    public int updateBsLottery(BsLottery bsLottery)
    {
        return bsLotteryMapper.updateBsLottery(bsLottery);
    }

    /**
     * 批量删除抽奖号码
     * 
     * @param lotteryIds 需要删除的抽奖号码主键
     * @return 结果
     */
    @Override
    public int deleteBsLotteryByLotteryIds(Long[] lotteryIds)
    {
        return bsLotteryMapper.deleteBsLotteryByLotteryIds(lotteryIds);
    }

    /**
     * 删除抽奖号码信息
     * 
     * @param lotteryId 抽奖号码主键
     * @return 结果
     */
    @Override
    public int deleteBsLotteryByLotteryId(Long lotteryId)
    {
        return bsLotteryMapper.deleteBsLotteryByLotteryId(lotteryId);
    }

    @Override
    public String importUser(List<BsLottery> userList, boolean updateSupport, String operName) {
        {
            if (StringUtils.isNull(userList) || userList.size() == 0)
            {
                throw new ServiceException("导入用户数据不能为空！");
            }
            int successNum = 0;
            int failureNum = 0;
            StringBuilder successMsg = new StringBuilder();
            StringBuilder failureMsg = new StringBuilder();
            for (BsLottery bsLottery : userList) {
                this.insertBsLottery(bsLottery);
            }
            return "导入成功".toString();
        }
    }

    @Override
    public List<String> onLottery(Long lotteryNums) {
        List<String> bsLotteries = null;
        if (lotteryNums==3){
            bsLotteries = bsLotteryMapper.onBsLotteryList(lotteryNums);
            bsLotteries.stream().filter(lottery->{
                BsLotteryHistory bsLotteryHistory = new BsLotteryHistory();
                bsLotteryHistory.setLotteryNum(lottery);
                return bsLotteryHistoryMapper.selectBsLotteryHistoryList(bsLotteryHistory).size()==0;
            }).forEach(lottery->{
                BsLotteryHistory bsLotteryHistory = new BsLotteryHistory();
                bsLotteryHistory.setLotteryNum(lottery);
                bsLotteryHistory.setCreateTime(new Date());
                bsLotteryHistory.setLotteryType("五火瀚雪张辽(3位)");
                bsLotteryHistoryMapper.insertBsLotteryHistory(bsLotteryHistory);
            });
        }
        if (lotteryNums==20){
            bsLotteries = bsLotteryMapper.onBsLotteryList(lotteryNums);
            bsLotteries.stream().forEach(lottery->{
                BsLotteryHistory bsLotteryHistory = new BsLotteryHistory();
                bsLotteryHistory.setLotteryNum(lottery);
                bsLotteryHistory.setCreateTime(new Date());
                bsLotteryHistory.setLotteryType("零火瀚雪张辽(20位)");
                bsLotteryHistoryMapper.insertBsLotteryHistory(bsLotteryHistory);
            });
        }
        return bsLotteries;
    }
}
